<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <!-- <a-col :span="24">
            <a-form-model-item label="员工名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
              <a-input :disabled=formDisabled v-model="model.employeeId" placeholder="请输入员工名称"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
              <select-team-input :query-type="'parent'" :disabled="formDisabled" v-model="model.teamId" :tdlx="3" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
              <j-select-depart :disabled="formDisabled" v-model="model.departId" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="岗位序列" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionCategory">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                v-model="model.positionCategory"
                dictCode="position_category"
                placeholder="请选择岗位序列"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职务级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionLevelId">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                placeholder="请选择职务级别"
                v-model="model.positionLevelId"
                :dictCode="positionLevelIdDict"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postCode">
              <j-search-select
                :disabled="formDisabled"
                v-model="model.postCode"
                :dict="positionIdDict"
                placeholder="请选择岗位"
              />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="是否虚拟岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isVirtual">
              <j-dict-select-tag :disabled="formDisabled" type="radio" v-model="model.isVirtual" dictCode="YES_OR_NO" placeholder="请选择是否是虚拟岗位" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="任职时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workingTime">
              <j-date
                :disabled="formDisabled"
                placeholder="请选择任职时间"
                v-model="model.workingTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否在岗" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDuty">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.isDuty"
                dictCode="YES_OR_NO"
                placeholder="请选择是否在岗"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea
                :disabled="formDisabled"
                v-model="model.remark"
                rows="4"
                placeholder="请输入备注"
                :max-length="500"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput';
  import JSearchSelect from '@/views/scy/components/JSearchSelect.vue'

  export default {
    name: 'ErpEmployeePostChangeRecordForm',
    components: {
      SelectTeamInput,
      JSearchSelect
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      var validatorPost = async (rule, value, callback) => {
        if (value) {
          const that = this;
          // 支持一个部门下多岗位，一实岗多虚岗
          let result = that.dataSource.filter(item => (item.departId == this.model.departId && item.isVirtual == '0'));
          console.log(result);
          // 新增
          if (result && result.length > 0 && this.model.isDuty == '1' && this.model.isVirtual == '0' && !this.id) {
            callback('当前部门已经存在任职的真实岗位！')
            return;
            // 编辑
          } else if (result && result.length > 0 && this.model.isDuty == '1' && this.model.isVirtual == '0') {
            if (result[0].id != this.id) {
              callback('当前部门已经存在任职的真实岗位！')
              return;
            }
          }
          let result2 = that.dataSource.filter(item => (item.departId == this.model.departId && item.postCode == this.model.postCode));
          if (result2 && result2.length > 0 && this.model.isDuty == '1' && !this.id) {
            callback('该部门下岗位不能重复！')
            return;
          } else if (result2 && result2.length > 0 && this.model.isDuty == '1') {
            if (result2[0].id != this.id) {
              callback('该部门下岗位不能重复！')
              return;
            }
          }
          callback()
        }
        callback()
      };
      return {
        model: {
          'isDuty': '1'
        },
        labelCol: {

        },
        id: '',
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        positionIdDict: 'sys_position,name,code,del_flag = 0 and audit_status=2 and is_virtual=0',
        positionLevelIdDict: 'erp_org_position_level,level_name,id,del_flag="0" and audit_status="2"',
        validatorRules: {
          teamId: [
          { required: true, message: '请选择团队!' }
        ],
        positionCategory: [
          { required: true, message: '请选择岗位序列!' }
        ],
        positionLevelId: [
          { required: true, message: '请选择职务级别!' }
        ],
        postCode: [
          { required: true, message: '请选择岗位!' },
          { validator: validatorPost, trigger: 'change' }
        ],
        departId: [
          { required: true, message: '请选择部门!' }
        ],
        isDuty: [
          { required: true, message: '请选择是否在岗!' }
        ],
        // isVirtual: [
        //   { required: true, message: '请选择是否虚拟岗位!' }
        // ],
        workingTime: [
          { required: true, message: '请选择任职时间!' }
        ]
        },
        url: {
          add: '/api/erp/files/employee/erpEmployeePostChangeRecord/add',
          edit: '/api/erp/files/employee/erpEmployeePostChangeRecord/edit',
          queryById: '/api/erp/files/employee/erpEmployeePostChangeRecord/queryById'
        },
        dataSource: []
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record, dataSource, id) {
        this.$refs.form.clearValidate()
        this.model = Object.assign({}, record);
        this.$set(this.model, 'postDataSource', 'employee') // 岗位的数据来源
        this.$set(this.model, 'isVirtual', '0') // 实岗
        this.dataSource = dataSource;
        this.id = id;
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            console.log('ErpEmployeePostChangeRecordForm', this.model)
            that.$emit('ok', that.model);
            // that.confirmLoading = true;
            // let httpurl = '';
            // let method = '';
            // if(!this.model.id){
            //   httpurl+=this.url.add;
            //   method = 'post';
            // }else{
            //   httpurl+=this.url.edit;
            //    method = 'put';
            // }
            // httpAction(httpurl,this.model,method).then((res)=>{
            //   if(res.success){
            //     that.$message.success(res.message);
            //     that.$emit('ok');
            //   }else{
            //     that.$message.warning(res.message);
            //   }
            // }).finally(() => {
            //   that.confirmLoading = false;
            // })
          }
        })
      }
    }
  }
</script>
